package com.hctg.forced2sleep.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.WallpaperManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;

import com.actor.myandroidframework.utils.LogUtils;
import com.actor.myandroidframework.utils.toaster.ToasterUtils;
import com.actor.picture_selector.utils.PictureSelectorUtils;
import com.bumptech.glide.Glide;
import com.hctg.forced2sleep.R;
import com.hctg.forced2sleep.databinding.ActivityWallpaperBinding;
import com.hctg.forced2sleep.service.MyWallpaperService;
import com.hctg.forced2sleep.utils.WallpaperUtils;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;

import java.util.ArrayList;
import java.util.List;

/**
 * description: 壁纸设置
 * @author    : ldf
 * @date      : 2024/5/2 on 12:22
 */
public class WallpaperActivity extends BaseActivity<ActivityWallpaperBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding.btnPermission.setOnClickListener(this::onViewClicked);
        viewBinding.btnSetRes.setOnClickListener(this::onViewClicked);
        viewBinding.btnSetBitmap.setOnClickListener(this::onViewClicked);
        viewBinding.btnSetVideo.setOnClickListener(this::onViewClicked);
    }

    @SuppressLint("WrongConstant")
    @Override
    public void onViewClicked(@NonNull View view) {
        switch (view.getId()) {
            case R.id.btn_permission:
                readWallPaper();
                break;
            case R.id.btn_set_res:      //设置R.drawable壁纸
                if (XXPermissions.isGranted(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    LogUtils.error("同意了壁纸设置权限1");
                    int id = WallpaperUtils.setResource(mActivity, R.drawable.ic_empty_for_layout_empty, WallpaperManager.FLAG_SYSTEM);
                    LogUtils.errorFormat("壁纸id: %d", id);
                } else {
                    XXPermissions.with(this)
                            .permission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
                            .request(new OnPermissionCallback() {
                                @Override
                                public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                                    LogUtils.error("同意了壁纸设置权限2");
                                    int id = WallpaperUtils.setResource(mActivity, R.drawable.ic_empty_for_layout_empty, WallpaperManager.FLAG_SYSTEM);
                                    LogUtils.errorFormat("壁纸id: %d", id);
                                }
                                @Override
                                public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                                    ToasterUtils.warning("您拒绝了壁纸设置权限!");
                                }
                            });
                }
                break;
            case R.id.btn_set_bitmap:   //设置Bitmap壁纸
                if (XXPermissions.isGranted(this, android.Manifest.permission.SET_WALLPAPER)) {
                    LogUtils.error("同意了壁纸设置权限3");
                    setBitmap();
                } else {
                    XXPermissions.with(this)
                            .permission(android.Manifest.permission.SET_WALLPAPER)
                            .request(new OnPermissionCallback() {
                                @Override
                                public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                                    LogUtils.error("同意了壁纸设置权限4");
                                    setBitmap();
                                }
                                @Override
                                public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                                    ToasterUtils.warning("您拒绝了壁纸设置权限!");
                                }
                            });
                }
                break;
            case R.id.btn_set_video:    //设置Video视频
                setVideo();
                break;
            default:
                break;
        }
    }

    private void readWallPaper() {
        if (XXPermissions.isGranted(this, Permission.READ_EXTERNAL_STORAGE)) {
            @SuppressLint("MissingPermission")
            Drawable wallpaperDesktop = WallpaperUtils.getWallpaperDesktop(this);
            Glide.with(this)
                    .load(wallpaperDesktop)
                    .into(viewBinding.ivWallpaper);
        } else {
            XXPermissions.with(this)
                    .permission(Permission.READ_EXTERNAL_STORAGE)
                    .request(new OnPermissionCallback() {
                        @Override
                        public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                            readWallPaper();
                        }
                        @Override
                        public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                            ToasterUtils.warning("您拒绝了权限, 读取不到壁纸!");
                            XXPermissions.startPermissionActivity(mActivity, Permission.READ_EXTERNAL_STORAGE);
                        }
                    });
        }
    }

    private void setBitmap() {
        PictureSelectorUtils.create(this, null)
                .selectImage(false)
                .setSingleSelect(true)
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        PictureSelectorUtils.printLocalMedia(result.get(0));
                        String sandboxPath = result.get(0).getSandboxPath();
                        if (sandboxPath == null) sandboxPath = result.get(0).getRealPath();
                        //设置图片
                        WallpaperUtils.setPictureWallPaper(mActivity, sandboxPath, MyWallpaperService.class);
                    }
                    @Override
                    public void onCancel() {
                    }
                });
    }

    private void setVideo() {
        PictureSelectorUtils.create(mActivity, null)
                .selectVideo()
                .setSingleSelect(true)
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        PictureSelectorUtils.printLocalMedia(result.get(0));
                        String sandboxPath = result.get(0).getSandboxPath();
                        if (sandboxPath == null) sandboxPath = result.get(0).getRealPath();
                        WallpaperUtils.setVideoWallPaper(mActivity, sandboxPath, MyWallpaperService.class);
                    }
                    @Override
                    public void onCancel() {
                    }
                });
    }
}